<?php
/**
 * PageController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/** action cacher */
require_once 'Madoqua/Cache/Action.php';

/**
 * BlogAdmin_PageController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_PageController extends Madoqua_Controller_Action_Abstract
{
    /**
     * page model
     *
     * @var Blog_Page
     */
    private $_pageModel;

    /**
     * init
     */
    public function init()
    {
        parent::init();
        //bubblez!

        $this->_modelLoader->load('Blog_Page');
        $this->_pageModel = new Blog_Page();
        //page model
    }

    /**
     * index... index... ja, index.
     */
    public function indexAction()
    {
    }

    /**
     * list recent pages
     */
    public function recentAction()
    {
        $count = (int) $this->_getParam('count', 10);
        //the number of posts to find
        $pages = $this->_pageModel->fetchAll(null, 'id DESC', $count);
        //fetch the latest $count posts

        $this->view->pages = $pages;
    }

    /**
     * Crrrrrreate, yes, create! (then rest on sunday)
     */
    public function createAction()
    {
        $form = $this->_formLoader->get('BlogAdmin_Page_Page');
        $this->view->form = $form;
        //d4 form! d4 form haz b33n lo4dz0rd!
        
        if ($form->isPrg() && $form->isValid($form->getPrgData())) {
            $page = $this->_pageModel->fetchNew();
            //the new post
            $page->setFromArray($form->getModelValues($this->_pageModel));
            //set the values
            $page->posted = time();
            //it was posted... right now!
            $page->save();
            //save it

            $this->updateCache();
            //update the action cache

            $this->getHelper('redirector')->gotoRoute(
                array(
                        'controller' => 'page', 
                        'action' => 'update',
                        'id' => $page->id
                ),
                'blog_admin'
            );
            //go away, you heathen!
        }
    }

    /**
     * update a page
     */
    public function updateAction()
    {
        $id = (int) $this->_getParam('id', 0);
        $page = $this->_pageModel->find($id)->current();
        if ($page == null) {
            throw new Exception('Post not found');
        }

        $form = $this->_formLoader->get('BlogAdmin_Page_Page');
        $this->view->form = $form;

        $form->populate($page->toArray());
        //populate the form

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            $page->setFromArray($form->getModelValues($this->_pageModel));
            //set the values
            $page->save();
            //save it

            $this->updateCache();
            //update the action cache

        }
    }

    /**
     * update the action cache
     */
    private function updateCache() {
        $cache = Madoqua_Cache_Action::get();
        //get the action cacher
        $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array(
            'controller_page' 
            ));
            //clear all page controller caches
    }
}